package com.catl.plc.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @ClassName Tool
 * @Date 2022/1/27 11:43
 * @Author 王东平
 */
public abstract class Tool {

    public static List<Integer> asList(int... ints) {
        List<Integer> intList = new ArrayList<>();
        for (int i : ints) {
            intList.add(i);
        }
        return intList;
    }

    /**
     * short[] 转 boolean[]
     *
     * @param shorts
     * @return
     */
    public static boolean[] shortToBool(short[] shorts) {
        if (shorts.length == 0) {
            return new boolean[0];
        }
        boolean[] booleans = new boolean[shorts.length];
        for (int i = 0; i < shorts.length; i++) {
            booleans[i] = shorts[i] == 1 ? true : false;
        }
        return booleans;
    }

    /**
     * 按指定长度拆分字符串
     *
     * @param str
     * @param length
     * @return
     */
    public static List<String> splitStrByLength(String str, int length) {
        List<String> list = new ArrayList<>();
        if (str.length() < length) {
            return list;
        }
        int count = str.length() % length == 0 ? str.length() / length : str.length() / length + 1;
        int[] remainder = new int[str.length() % length];
        StringBuffer remainderBuf = new StringBuffer("");
        for (int i = 0; i < remainder.length; i++) {
            remainderBuf.append(remainder[i]);
        }
        str = remainderBuf.toString()+str;
        for (int i = 0; i < count; i++) {
            list.add(str.substring(i * length, (i + 1) * length));
        }
        if (CollectionUtils.isEmpty(list)) {
            throw new NullPointerException("结果不能null ！！！！");
        }
        return list;

    }

    /**
     * short 转 二进制字符串
     *
     * @param sh
     * @return
     */
    public static String shortToBinString(long sh, int formatLength) {
        String str = String.format("%" + formatLength + "s", Long.toBinaryString(sh)).replaceAll(" ", "0");
        return str.substring(str.length()-formatLength);
    }

    public static String shorts2BinStr(List<Integer> readResult) {
        if (CollectionUtils.isEmpty(readResult)) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder("");
        for (Integer value : readResult) {
            stringBuilder.append(shortToBinString(value, 16));
        }
        return stringBuilder.toString();
    }

    /**
     * short[] 转成 二进制字符字符串
     *
     * @param readResult
     * @return
     */
    public static String shorts2BinStr(short[] readResult) {
        if (readResult == null) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder("");
        for (short value : readResult) {
            stringBuilder.append(shortToBinString(value, 16));
        }
        return stringBuilder.toString();
    }

    /**
     * short[] 转成 二进制字符集合
     */
    public static List<char[]> shorts2BinChars(List<Integer> readResult) {
        List<char[]> decodes = new ArrayList<>();
        if (CollectionUtils.isEmpty(readResult)) {
            return decodes;
        }
        for (Integer value : readResult) {
            String str = shortToBinString(value, 16);
            decodes.add(str.toCharArray());
        }
        return decodes;
    }

    /**
     * 去除前缀 0
     */
    public static String trimZero(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        str = str.replaceAll("^(0+)", "");
        str = str.equals("") ? "0" : str;
        return str;
    }
}
